package demo1;

import java.util.LinkedList;
import java.util.Queue;

public class MyStack1 {
    private Queue<Integer> queue1;
    private Queue<Integer> queue2;
        public MyStack1() {
          this.queue1 = new LinkedList<>();
          this.queue2 = new LinkedList<>();
        }
    // 将元素 x 压入栈顶。
        public void push(int x) {
          if(!queue2.isEmpty()){
              queue2.offer(x);
          }else {
              queue1.offer(x);
          }
        }
    //移除并返回栈顶元素。
        public int pop() {
            if(empty()){
                return -1;
            }
            if(queue1.isEmpty()){
                int size = queue2.size();
                while(size - 1 != 0){
                    queue1.offer(queue2.poll());
                    size--;
                }
                return queue2.poll();
            }else {
                int size = queue1.size();
                while(size - 1 != 0){
                    queue2.offer(queue1.poll());
                    size--;
                }
                return queue1.poll();
            }
        }
    //返回栈顶元素。
        public int top() {
            int val = -1;
            if(empty()){
                return -1;
            }
            if(!queue1.isEmpty()){
                int size = queue1.size();
                while(size != 0){
                    val = queue1.poll();
                    queue2.offer(val);
                    size--;
                }
            }else {
                int size = queue2.size();
                while(size != 0){
                    val = queue2.poll();
                    queue1.offer(val);
                    size--;
                }
            }
            return val;
        }

        public boolean empty() {
         return queue1.isEmpty() && queue2.isEmpty();
        }
}
